package org.lemon.cla.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableLogic; // 导入 TableLogic
import com.baomidou.mybatisplus.annotation.TableField; // 导入 TableField (如果is_deleted对应数据库字段名)

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import org.lemon.cla.enums.UserRoleEnum;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class User implements UserDetails {

    @TableId(type = IdType.AUTO)
    private Integer userId;

    private String username;

    private String avatarUrl;

    private String password;

    private UserRoleEnum role;

    private String realName;

    private String phone;

    private String email;

    private LocalDateTime createTime;

    private LocalDateTime lastLogin;

    @TableLogic // <-- 关键：启用 MyBatis-Plus 的逻辑删除功能
    @TableField("is_deleted") // <-- 确保字段名与数据库匹配
    private Boolean isDeleted; // 数据库中通常 TINYINT(1) 对应 Boolean，默认值应为 0 (false)

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of(new SimpleGrantedAuthority("ROLE_" + this.role.getDescription()));
    }
    @Override
    public boolean isAccountNonExpired() { return true; }
    @Override
    public boolean isAccountNonLocked() { return true; }
    @Override
    public boolean isCredentialsNonExpired() { return true; }
    @Override
    public boolean isEnabled() { return true; }
}